//
//  SSCommentsView.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/9/19.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSCommentsView.h"

@interface SSCommentsView () <UITextFieldDelegate>

@property (nonatomic,strong) UIView *maskView;
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UITextField *commentTF;

@end

@implementation SSCommentsView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kRGBColor(244, 244, 244);
        [self setupUI];
    }
    return self;
}

- (void) setupUI {
    
    UITextField *textField = [[UITextField alloc] init];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = @"赶快写上你的评论吧";
    textField.font = kFont(14);
    textField.delegate = self;
    [self addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.centerY.mas_equalTo(self);
        make.height.mas_equalTo(30);
    }];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];


}

- (void)keyboardWillChangeFrame:(NSNotification *)note {

    // 1.获取键盘frame
    CGRect endFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    NSLog(@"%f",endFrame.origin.y);
    
    // 2.获取键盘弹出时长
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [UIView animateWithDuration:duration animations:^{
        [self.backView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(-endFrame.origin.y + 64);
        }];
        [self.backView.superview layoutIfNeeded];

    } completion:^(BOOL finished) {
        
    }];

}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    [self show];
    [textField resignFirstResponder];
}

- (void) show {
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self.maskView];

    [self.maskView addSubview:self.backView];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.maskView);
        make.height.mas_equalTo(44);
    }];

    
    [self.backView addSubview:self.commentTF];
    [self.commentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(self.backView);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.commentTF becomeFirstResponder];
    });
    
    

}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void) dismiss {
    [self.commentTF resignFirstResponder];
    [self.maskView removeFromSuperview];
    self.maskView = nil;
}

- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2f];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        [_maskView addGestureRecognizer:tap];
    }
    return _maskView;
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = kRGBColor(244, 244, 244);
    }
    return _backView;
}

- (UITextField *)commentTF {
    if (!_commentTF) {
        _commentTF = [[UITextField alloc] init];
        _commentTF.borderStyle = UITextBorderStyleRoundedRect;
        _commentTF.placeholder = @"期待你的评论";
        _commentTF.font = kFont(14);
        
    }
    return _commentTF;

}

@end
